package org.manager.properties;

import java.io.Serializable;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.manager.generic.web.JsonViewUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/properties")
public class PropertiesController {

	@Resource(name="propertiesRepository")
	private PropertiesRepository pr;
	
	private static final String propertiesAllField[] = new String[]{"id","englishName","propertyCode","propertyId","code","name","type","order","status","isedit","parentCode","parentId","businessCode","level","creater","createdTime","createrName"};
	
	@RequestMapping(params="method=page")
	public ModelAndView page(String propertyId, String parentId, String start, String limit){
		
		int istart = 0;
		int ilimit = 10;
		if(NumberUtils.isNumber(start)){
			istart = NumberUtils.toInt(start);
			if(istart > 1){
				istart = istart-1;
			}
		}
		
		if(NumberUtils.isNumber(limit) && NumberUtils.toInt(limit)>0){
			ilimit = NumberUtils.toInt(limit);
		}
		
		Properties pro = new Properties();
		if(StringUtils.isNotBlank(propertyId)){
			pro.setPropertyId(propertyId);
		}else{
			pro.setParentId(parentId);
		}
		
		
		Map<String,Object> map = JsonViewUtil.getSuccessMap();
		map.put("totalProperty", pr.totalRecord(pro));
		map.put( "data", pr.page(pro, istart/1, ilimit/1));
		map.put("start", istart);
		map.put("limit", ilimit);
		
		return JsonViewUtil.getSuccessViewWithIncludProperty(map, propertiesAllField);
	}
	
	
	@RequestMapping(params="method=add", method=RequestMethod.POST)
	public ModelAndView add(Properties pro){
		if(pro != null ){
			if(StringUtils.isBlank(pro.getPropertyId())){
				pro.setPropertyId(null);
			}
			if(StringUtils.isBlank(pro.getParentId())){
				pro.setParentId(null);
			}else{
				pro.setPropertyId(null);
			}
		}
		
		Serializable id = null;
		if(pr.checkBusinessCode(pro)){
			id = pr.addProperties(pro);
		}
		
		if(id != null && !id.equals("")){
			return JsonViewUtil.getSuccessView();
		}else{
			return JsonViewUtil.getFailureView();
		}
	}
	
	
	@RequestMapping(params="method=loadById")
	public ModelAndView loadById(String id){
		Properties pro = pr.get(id);
		if(pro != null){
			return JsonViewUtil.getSuccessViewWithIncludProperty(pro, propertiesAllField);
		}else{
			return JsonViewUtil.getFailureView();
		}
	}
	
	
	@RequestMapping(params="method=modify", method=RequestMethod.POST)
	public ModelAndView modify(Properties pro){
		if(pro != null ){
			if(StringUtils.isBlank(pro.getPropertyId())){
				pro.setPropertyId(null);
			}
			if(StringUtils.isBlank(pro.getParentId())){
				pro.setParentId(null);
			}
		}
		
		if(pro != null && StringUtils.isNotBlank(pro.getId())){
			pr.update(pro);
			return JsonViewUtil.getSuccessView();
		}else{
			return JsonViewUtil.getFailureView();
		}
	}
	
	@RequestMapping(params="method=del")
	public ModelAndView del(String ids){
		if(StringUtils.isNotBlank(ids)){
			String id[] = ids.split(",");
			for(String s : id){
				pr.deleteByKey(s);
			}
			return JsonViewUtil.getSuccessView();
		}else{
			return JsonViewUtil.getFailureView();
		}
	}
	
	@RequestMapping(params="method=selectByParent")
	public ModelAndView selectByParent(String node, String type){
		if(StringUtils.isBlank(type)){
			if(StringUtils.isBlank(node)){
				return JsonViewUtil.getFailureView();
			}else{
				if(node.equals("0")){
					return JsonViewUtil.getSuccessView();
				}
				return JsonViewUtil.getSuccessViewWithIncludProperty(pr.selectByParent(node), propertiesAllField);
			}
		}else{
			return JsonViewUtil.getSuccessViewWithIncludProperty(pr.selectByProperty(node), propertiesAllField);
		}
		
	}
	
}
